An array of n
integers is given. Find the number of maximum elements in the array.
Input. The first line
contains the number n (n ≤ 100) – the number of elements in
the array. The second line contains n integers, each with an absolute
value not exceeding 100.
Output. Print the number of
maximum elements in the array.
Sample input |
Sample output |
5 1 6 2 6 2 |
2 |
array
Algorithm analysis
Find the maximum element of the array. Then, perform a
second pass through the array to count the number of maximum elements.
Algorithm implementation
Declare an array.
int m[101];
Read the input array. Find its maximum element max.
scanf("%d", &n);
max = -2000000000;
for (i = 0; i < n; i++)
{
scanf("%d", &m[i]);
if (m[i] > max) max = m[i];
}
In the variable cnt, count the number of maximum elements.
cnt = 0;
for (i = 0; i < n; i++)
if (m[i] == max) cnt++;
Print the answer.
printf("%d\n", cnt);
Python implementation
Read the input data.
n = int(input())
lst = list(map(int, input().split()))
Find the maximum element mx in the list lst.
mx = max(lst)
Store the count of maximum elements in the variable res.
res = lst.count(mx)
Print the answer.
print(res)